﻿Imports System.Deployment.Application
Imports System.Resources
Imports System.Globalization
Imports System.Threading


Public Class Main

    Dim Number As Double
    Dim Times As Double
    Dim By As Double
    Dim Quit As Boolean
    Dim Wrong1 As Boolean = False
    Dim Wrong2 As Boolean = False
    Dim Wrong3 As Boolean = False
    Dim UpdateProgram As Boolean = True
    Dim LocRM As New ResourceManager("Counter_Generator.Main", GetType(Main).Assembly)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox2.Text = "" Or TextBox3.Text = "" Then
            MessageBox.Show(LocRM.GetString("no_number"), LocRM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error)
            GoTo End2
        End If

        If TextBox2.Text.Length > 8 Or TextBox3.Text.Length > 8 Or TextBox4.Text.Length > 8 Then
            MessageBox.Show(LocRM.GetString("would_crash"), LocRM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error)
            GoTo End2
        End If

        If Not IsNumeric(TextBox2.Text) Or Not IsNumeric(TextBox3.Text) Then
            MessageBox.Show(LocRM.GetString("numbs_only"), LocRM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error)
            GoTo End2
        End If
        Times = TextBox3.Text
        Number = TextBox2.Text
        If TextBox4.Text = "" Or Not IsNumeric(TextBox4.Text) Then
            By = 1
            TextBox4.Text = "1"
        Else
            By = TextBox4.Text
            If By > Times And TextBox4.Text > 0 Then
                MessageBox.Show(LocRM.GetString("third_bigger_second"), LocRM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error)
                GoTo End2
            End If
        End If

        If InStr(String.Format("{0}", (Number + Times) / By), ".") Or InStr(String.Format("{0}", (Number + Times) / By), ",") Then
            MessageBox.Show(LocRM.GetString("cannot_divide_by_to"), LocRM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error)
            GoTo End2
        End If

        If By = 0 Then
            MessageBox.Show(LocRM.GetString("increment_0"), LocRM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error)
            GoTo End2
        End If

        If TextBox4.Text < 0 Then
            If Times > Number Then
                MessageBox.Show(LocRM.GetString("first_lower_second"), LocRM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error)
                GoTo End2
            End If
        Else
            If Times < Number Then
                MessageBox.Show(LocRM.GetString("second_lower_first"), LocRM.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error)
                GoTo End2
            End If
        End If

        Me.UseWaitCursor = True
        Me.Text = "Counter Generator - " & String.Format("Version {0}", My.Application.Deployment.CurrentVersion.ToString) & " (counting in progress)"
        If TextBox4.Text < 0 Then
            By = -By
            While Number > Times
                If Number = TextBox2.Text Then
                    TextBox1.Text = Number
                Else
                    TextBox1.AppendText(", " & Number)
                End If

                Number -= By
            End While
        Else
            While Number < Times
                If Number = TextBox2.Text Then
                    TextBox1.Text = Number
                Else
                    TextBox1.AppendText(", " & Number)
                End If

                Number += By
            End While
        End If
        TextBox1.AppendText(", " & Number & ".")
        Number = TextBox2.Text
        Button2.Enabled = True
        Me.UseWaitCursor = False
        Me.Text = "Counter Generator - " & String.Format("Version {0}", My.Application.Deployment.CurrentVersion.ToString)
        MessageBox.Show(LocRM.GetString("count_done"), LocRM.GetString("done"), MessageBoxButtons.OK)
End2:
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Clipboard.Clear()
        Clipboard.SetText(TextBox1.Text)
        MessageBox.Show(LocRM.GetString("content_copied"), LocRM.GetString("copied"), MessageBoxButtons.OK, MessageBoxIcon.Information)
        Button2.Enabled = False
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        About.Show()
        Me.Enabled = False
    End Sub

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        Button2.Enabled = False
        MessageBox.Show("Look at that! Like a new!", "Cleared", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub Main_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Quit = False Then
            Dim Confirm As Integer

            Confirm = MessageBox.Show("Are you sure you want to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

            If Confirm = vbYes Then
                Quit = True
                Me.Close()
            Else
                e.Cancel = True
                Quit = False
            End If
        End If
    End Sub

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = "Counter Generator - " & String.Format("Version {0}", My.Application.Deployment.CurrentVersion.ToString)
        Label4.Text = String.Format(Label4.Text, My.Application.Deployment.CurrentVersion.ToString)
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If Wrong1 = False And Wrong3 = False Then
            If IsNumeric(TextBox2.Text) Or TextBox2.Text = "" Then
                Button1.Enabled = True
                Label2.Visible = False
                Wrong2 = False
            Else
                Button1.Enabled = False
                Label2.Visible = True
                Wrong2 = True
            End If
        End If
    End Sub

    Private Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        If Wrong2 = False And Wrong3 = False Then
            If IsNumeric(TextBox3.Text) Or TextBox3.Text = "" Then
                Button1.Enabled = True
                Label2.Visible = False
                Wrong1 = False
            Else
                Button1.Enabled = False
                Label2.Visible = True
                Wrong1 = True
            End If
        End If
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        If Wrong1 = False And Wrong2 = False Then
            If IsNumeric(TextBox4.Text) Or TextBox4.Text = "" Then
                Button1.Enabled = True
                Label2.Visible = False
                Wrong3 = False
            Else
                Button1.Enabled = False
                Label2.Visible = True
                Wrong3 = True
            End If
        End If
        If IsNumeric(TextBox4.Text) Then
            Dim value As Double
            value = TextBox4.Text
            If value < 0 Then
                Label6.Visible = True
            Else
                Label6.Visible = False
            End If
        End If
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        WhatsNew.Show()
        Me.Enabled = False
    End Sub

    Private Sub UpdateIsAvailableToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateIsAvailableToolStripMenuItem.Click
        InstallUpdateSyncWithInfo()
    End Sub

    Private Sub UpdateTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateTimer.Tick
        UpdateTimer.Enabled = False
        If UpdateProgram = True Then
            InstallUpdateSyncWithInfo()
            UpdateTimer.Enabled = True
        End If
    End Sub

    Private Sub InstallUpdateSyncWithInfo()
        Dim info As UpdateCheckInfo = Nothing

        If (ApplicationDeployment.IsNetworkDeployed) Then
            Dim AD As ApplicationDeployment = ApplicationDeployment.CurrentDeployment

            Try
                info = AD.CheckForDetailedUpdate()
            Catch dde As DeploymentDownloadException
                MessageBox.Show("The new version of the application cannot be downloaded at this time. " + ControlChars.Lf & ControlChars.Lf & "Please check your network connection, or try again later. Error: " + dde.Message)
                Return
            Catch ioe As InvalidOperationException
                MessageBox.Show("This application cannot be updated. It is likely not a ClickOnce application. Error: " & ioe.Message)
                Return
            End Try

            If (info.UpdateAvailable) Then
                Dim doUpdate As Boolean = True
                UpdateIsAvailableToolStripMenuItem.Visible = True

                If (Not info.IsUpdateRequired) Then
                    Dim dr As DialogResult = MessageBox.Show("An update is available. Would you like to update the application to version " & info.AvailableVersion.ToString() & " now?", "Update Available", MessageBoxButtons.YesNo)
                    If (Not System.Windows.Forms.DialogResult.Yes = dr) Then
                        doUpdate = False
                        UpdateProgram = False
                    End If
                Else
                    ' Display a message that the app MUST reboot. Display the minimum required version.
                    MessageBox.Show("This application has detected a mandatory update from your current " & _
                        "version to version " & info.MinimumRequiredVersion.ToString() & _
                        ". The application will now install the update and restart.", _
                        "Update Needed", MessageBoxButtons.OK, _
                        MessageBoxIcon.Information)
                End If

                If (doUpdate) Then
                    Try
                        AD.Update()
                        MessageBox.Show("The application has been upgraded, and will now restart.")
                        Quit = True
                        Application.Restart()
                    Catch dde As DeploymentDownloadException
                        MessageBox.Show("Cannot install the latest version of the application. " & ControlChars.Lf & ControlChars.Lf & "Please check your network connection, or try again later.")
                        Return
                    End Try
                End If
            Else
                UpdateIsAvailableToolStripMenuItem.Visible = False
            End If
        End If
    End Sub
End Class
